package com.jh.channel.business.impl;

import com.jh.channel.business.FastSilverBusiness;
import com.jh.channel.pojo.fastsilver.*;
import com.jh.channel.repository.FastSilverInternalRepository;
import com.jh.channel.repository.FastSilverRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.util.StringUtils.hasText;

@Service
public class FastSilverBusinnessImpl implements FastSilverBusiness {

    private final FastSilverRepository fastSilverRepository;
    private final FastSilverInternalRepository fastSilverInternalRepository;

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public FastSilverBusinnessImpl(FastSilverRepository fastSilverRepository,
                                   FastSilverInternalRepository fastSilverInternalRepository,
                                   JdbcTemplate jdbcTemplate) {
        this.fastSilverRepository = fastSilverRepository;
        this.fastSilverInternalRepository = fastSilverInternalRepository;
        this.jdbcTemplate = jdbcTemplate;
    }

    public Page<FastSilverOrderView> queryFastSilver(FastSilverOrderView view,Pageable pageable) {
        StringBuilder sql = new StringBuilder();
        sql.append("select order_type,merchant_name,user_id,order_status,money,order_no,device,goods_msg,out_trade_no,update_time,create_time from t_fastsilver_order where 1=1");
        if (hasText(view.getOrderNo())) {
            sql.append(" and order_no = " + view.getOrderNo());
        }

        if (view.getOrderType() != null) {
            sql.append(" and order_type = '" + view.getOrderType() + "'");
        }
        if (view.getUserId() > 0) {
            sql.append(" and user_id = " + view.getUserId());
        }

        if (view.getOrderStatus() != null) {
            sql.append(" and order_status = '" + view.getOrderStatus() + "'");
        }

        if (hasText(view.getStartDate())) {
            sql.append(" and create_time >= " + view.getStartDate());
        }

        if (hasText(view.getEndDate())) {
            sql.append(" and create_time <= " + view.getEndDate());
        }

        if (hasText(view.getMinAmount())) {
            sql.append(" and money >= " + view.getMinAmount());
        }

        if (hasText(view.getMaxAmount())) {
            sql.append(" and money <= " + view.getMaxAmount());
        }
        List<FastSilverOrder> listCount = jdbcTemplate.query(sql.toString(),new Object[]{},new BeanPropertyRowMapper<FastSilverOrder>(FastSilverOrder.class));


        sql.append( " order by create_time desc limit " + pageable.getOffset() + "," + pageable.getPageSize());
        List<FastSilverOrder> list = jdbcTemplate.query(sql.toString(),new Object[]{},new BeanPropertyRowMapper<FastSilverOrder>(FastSilverOrder.class));

        if (list.isEmpty()) {
            List<FastSilverOrderView> result = new ArrayList<>();
            result.add(new FastSilverOrderView());
            return new PageImpl<>(result, pageable, 0);
        }
        List<FastSilverOrderView> result = new ArrayList<>();
        for (FastSilverOrder fastSilverOrder : list) {
            result.add(fastSilverOrder.transform());
        }
        return new PageImpl<>(result, pageable, listCount.size());
    }
    public List<FastSilverInternal> queryInternal(String userId,String merchantId,Pageable pageable)throws ParseException {
        StringBuilder sql = new StringBuilder();
        sql.append("select user_id,order_no,shop_name,nickname,merchant_name,phone,artif_name,artif_phone,status,rate_wx,rate_alipay,merchant_id,create_time from t_fastsilver_internal where 1=1");
        if (hasText(userId)) {
            sql.append(" and user_id = " + userId);
        }
        if (hasText(merchantId)) {
            sql.append(" and merchant_id = '" + merchantId + "' ");
        }
        sql.append( " order by create_time desc limit " + pageable.getOffset() + "," + pageable.getPageSize());

        List<FastSilverInternal> list = jdbcTemplate.query(sql.toString(),new Object[]{},new BeanPropertyRowMapper<FastSilverInternal>(FastSilverInternal.class));

        return list;
    }

    public List<FastSilverInternal> queryInternalcount(String userId,String merchantId)throws ParseException {
        StringBuilder sql = new StringBuilder();
        sql.append("select user_id,order_no,shop_name,nickname,merchant_name,phone,artif_name,artif_phone,status,rate_wx,rate_alipay,merchant_id,create_time from t_fastsilver_internal where 1=1");
        if (hasText(userId)) {
            sql.append(" and user_id = " + userId);
        }
        if (hasText(merchantId)) {
            sql.append(" and merchant_id = '" + merchantId + "' ");
        }
        List<FastSilverInternal> list = jdbcTemplate.query(sql.toString(),new Object[]{},new BeanPropertyRowMapper<FastSilverInternal>(FastSilverInternal.class));

        return list;
    }

    @Override
    public String queryUseridByOrderId() {
        return null;
    }


    public void updateOrderStatus(String orderNo,OrderStatus orderStatus,String outTradeNo,String resultMsg) {
        FastSilverOrder order = fastSilverRepository.findByOrderNo(orderNo);
        if (order != null) {
            order.setOutTradeNo(outTradeNo);
            order.setOrderStatus(orderStatus);
            fastSilverRepository.save(order);
        }

    }



}
